package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.bac.BacWorkFlow;
import com.ysd.lis.entity.bac.BacWorkFlowNode;
import com.ysd.lis.entity.bac.BacWorkFlowNodeDict;
import com.ysd.lis.mapper.bac.BacWorkFlowMapper;
import com.ysd.lis.mapper.bac.BacWorkFlowNodeDictMapper;
import com.ysd.lis.mapper.bac.BacWorkFlowNodeMapper;
import com.ysd.lis.service.bac.BacWorkFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 流程设定主表 服务实现类
 * </p>
 *
 * @author cfz
 * @since 2024-04-09
 */
@Service
public class BacWorkFlowServiceImpl extends ServiceImpl<BacWorkFlowMapper, BacWorkFlow> implements BacWorkFlowService {
    @Autowired
    BacWorkFlowMapper bacWorkFlowMapper;
    @Autowired
    BacWorkFlowNodeDictMapper bacWorkFlowNodeDictMapper;
    @Autowired
    BacWorkFlowNodeMapper bacWorkFlowNodeMapper;

    @Override
    public Result findBacWorkFlowList(BacWorkFlow bacWorkFlow) {
        List<BacWorkFlow> list = findBacWorkFlowArray(bacWorkFlow);
        return Result.succ(1, "请求成功", list);
    }

    @Override
    public List<BacWorkFlow> findBacWorkFlowArray(BacWorkFlow bacWorkFlow) {
        MPJLambdaWrapper<BacWorkFlow> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(BacWorkFlow::getDelFlag, 0);
        queryWrapper.eq(StringUtils.isNotBlank(bacWorkFlow.getId()), BacWorkFlow::getId, bacWorkFlow.getId());
        return bacWorkFlowMapper.selectList(queryWrapper);
    }

    @Override
    public Result addBacWorkFlow(BacWorkFlow bacWorkFlow) {
        int i = bacWorkFlowMapper.insert(bacWorkFlow);
        if (i > 0) {
            return Result.succ(1, "新增成功", bacWorkFlow);
        }
        return Result.fail("新增失败");
    }

    @Override
    public Result deleteBacWorkFlow(BacWorkFlow bacWorkFlow) {
        bacWorkFlow.setDelFlag(1);
        int i = bacWorkFlowMapper.updateById(bacWorkFlow);
        if (i > 0) {
            return Result.succ(1, "删除成功", bacWorkFlow);
        }
        return Result.fail("删除失败");
    }

    @Override
    public Result updateBacWorkFlow(BacWorkFlow bacWorkFlow) {
        int i = bacWorkFlowMapper.updateById(bacWorkFlow);
        if (i > 0) {
            return Result.succ(1, "更新成功", bacWorkFlow);
        }
        return Result.fail("更新失败");
    }

    @Override
    public Result selectProsettingNode(String settingId) {
        //查询出中间表的节点信息
        Map map = new HashMap<>();
        MPJLambdaWrapper<BacWorkFlowNode> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(BacWorkFlowNode.class)
                .selectAs(BacWorkFlowNodeDict::getNodeTypeNa, BacWorkFlowNode::getNodeTypeNa)
                .selectAs(BacWorkFlowNodeDict::getExplain, BacWorkFlowNode::getExplain)
                .selectAs(BacWorkFlowNodeDict::getPageName, BacWorkFlowNode::getPageName)
                .leftJoin(BacWorkFlowNodeDict.class, p -> p.eq(BacWorkFlowNodeDict::getDelFlag,0).eq(BacWorkFlowNodeDict::getCode,BacWorkFlowNode::getNodeDictNo))
                .eq(BacWorkFlowNode::getFlowNo,settingId)
                .eq(BacWorkFlowNode::getDelFlag,0)
                .orderByAsc(BacWorkFlowNode::getSeq);
        List<BacWorkFlowNode> bacWorkFlowNodes = bacWorkFlowNodeMapper.selectList(lambdaQueryWrapper);
        List<String> collect = bacWorkFlowNodes.stream().map(BacWorkFlowNode::getNodeDictNo).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(collect)){
//            LambdaQueryWrapper<BacWorkFlowNodeDict> lambdaQueryWrapper1 = new LambdaQueryWrapper();
//            lambdaQueryWrapper1.in(BacWorkFlowNodeDict::getCode,collect).orderByAsc(BacWorkFlowNodeDict::getSeq);
//            List<BacWorkFlowNodeDict> bacWorkFlowNodeDicts = bacWorkFlowNodeDictMapper.selectList(lambdaQueryWrapper1);

            map.put("workFlowList", bacWorkFlowNodes);

            LambdaQueryWrapper<BacWorkFlowNodeDict> lambdaQueryWrapper2 = new LambdaQueryWrapper();
            lambdaQueryWrapper2.notIn(BacWorkFlowNodeDict::getCode,collect);
            lambdaQueryWrapper2.orderByAsc(BacWorkFlowNodeDict::getSeq);

            List<BacWorkFlowNodeDict> bacWorkFlowNodeDicts1 = bacWorkFlowNodeDictMapper.selectList(lambdaQueryWrapper2);
            map.put("workFlowNodeList", bacWorkFlowNodeDicts1);
        } else {
            List<BacWorkFlowNodeDict> bacWorkFlowNodeDicts = bacWorkFlowNodeDictMapper.selectList(new LambdaQueryWrapper<BacWorkFlowNodeDict>().orderByAsc(BacWorkFlowNodeDict::getSeq));
            map.put("workFlowList", new ArrayList<>());
            map.put("workFlowNodeList", bacWorkFlowNodeDicts);
        }
        return Result.succ(map);
    }

    @Override
    public BacWorkFlow findOneByNo(String no) {
        if (StringUtils.isBlank(no)) {
            return null;
        }
        LambdaQueryWrapper<BacWorkFlow> query = new LambdaQueryWrapper<>();
        query.eq(BacWorkFlow::getDelFlag, 0);
        query.eq(BacWorkFlow::getWorkFlowCode, no);
        List<BacWorkFlow> lst = bacWorkFlowMapper.selectList(query);
        if (!lst.isEmpty()) {
            return lst.get(0);
        }
        return null;
    }
}
